Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
AsyncStorageをNodeのREPLから操作するためにTop-levelでawaitし...
Search
joe_re
July 14, 2017
Technology
540
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
AsyncStorageをNodeのREPLから操作するためにTop-levelでawaitしたい人生だった
五反田.js #8 LT資料
joe_re
July 14, 2017
More Decks by joe_re
See All by joe_re
Eyes on Claude Code
joere
0
150
Building Public API with GraphQL
joere
3
160
Traversing the GraphQL AST and Calculating Query Costs
joere
0
1.3k
Real-Time applications with GraphQL
joere
0
320
Prisma2 with Graphql
joere
3
1k
Go beyound static on Netlify
joere
1
390
Building Real-time Vue App
joere
4
4.8k
ReactNativeのAsyncStorageをNodeのReplから操作する
joere
0
370
Mock Native API in your E2E test
joere
2
1.3k
Other Decks in Technology
See All in Technology
自主式軟體工廠
philipz
0
140
個別開発で終わらせない。 現場の課題をプロダクトの強さに変える StockmarkのFDE
ktkrhr
0
560
雪かき部 #7 もう怖くない!SELECT文!
foursue
0
270
OSC2026on_the-world-is-waiting-for-your-voice.pdf
naruoga
0
320
BedrockとLambdaで作る リアルタイム進行型推理ゲーム
kawametho
0
170
AIに攻撃される前に、AIに攻撃させる
tsuchikazu
0
130
SDDの運用にめげずに向き合った話
sansantech
PRO
0
110
SREでアラート疲れを 解決しよう!
kairim0
1
120
顧客の成果創出とプロダクトの成長を 両立するためのFDE
sansantech
PRO
0
640
Claude起点の仕様駆動開発
tanakaseiya
0
320
おそらく日本で唯一のDevRelインターン生として
husengs7
0
150
FinTech 1-2 : Overview of FinTech
ks91
PRO
0
130
Featured
See All Featured
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Exploring anti-patterns in Rails
aemeredith
4
520
Context Engineering - Making Every Token Count
addyosmani
9
1.2k
Building Applications with DynamoDB
mza
96
7.2k
Chasing Engaging Ingredients in Design
codingconduct
0
340
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
270
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Optimizing for Happiness
mojombo
378
71k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Transcript
AsyncStorage をNode の REPL から操作するために Toplevel でawait したい 人生だった @joe_re
Who am I? twitter: @joe_re github: @joere working in freee.K.K
What is AsyncStorage? ReactNative の標準で搭載されたkeyvalue ストレージ 気軽にReactNative に永続化したいデータを持つときに便利 ReactNative で使えるLocalStorage
みたいなもの
Example Persisting date: try { await AsyncStorage.setItem('@MySuperStore:key', 'I like to
save it.'); } catch (error) { // Error saving data } Fetching date: try { const value = await AsyncStorage.getItem('@MySuperStore:key'); if (value !== null){ // We have data!! console.log(value); } } catch (error) { // Error retrieving data }
AsyncStorage をNode のREPL から操作したいという欲望 AsyncStorage には外部から叩けるAPI は用意されていない 開発中にデータ構造が変わったときにアプリケーションに データを修正するためのコードを書くのは面倒 ちゃんとデータが保存されたかどうか確かめるのも面倒
REPL からAsyncStorage のAPI を叩きたい (rails console みたいなイメージ)
作った https://github.com/joere/asyncstoragerepl
DEMO
仕組み Node とReactNative との通信はWebsocket で行う REPL 起動時に子プロセスとしてReactNative アプリケーションと通信するWSS を立ち上げる REPL
とWSS はプロセス間通信を行う
イメージ図
ん?
どうしてこうなった
Node.js ではTop Level で非同期処理の 結果を扱えない 人類はES2017 よりasync/await を獲得したが、 await はasync
function の中でしか使えない // ng function request { const response = await fetch( '/data.json' ); const json = await response.json(); const result = await hydrateSomehow(json); return result; } // ok function async request { const response = await fetch( '/data.json' ); const json = await response.json(); const result = await hydrateSomehow(json); return result; }
REPL で Promise オブジェクトを 扱おうとすると.. > let out = null;
> promiseObj.then((res) => { out = res }); > out // finally available here
だるすぎワロた
Toplevel await is a footgun https://gist.github.com/Rich Harris/0b6f317657f5167663b493c722647221 Toplevel のawait を許すと1
つの依存を解決するだけでも、 その先の関係のない処理まで止めざるを得ない await は直列を強要するので、ネットワークを介した処理の 並列化ができない
Example(borrow from Harris) // main.js import './foo.js'; import './bar.js'; //
foo.js await delay( Math.random() * 1000 ); console.log( 'foo happened' ); // bar.js await delay( Math.random() * 1000 ); console.log( 'bar happened' );
将来的にはToplevel await が できるかも? https://github.com/tc39/ecmascriptasyncawait/issues/9
それはそれとしてNode のREPL に 例外的に入る可能性も https://github.com/nodejs/node/issues/13209
今回はこのような実装に _resolveMessageFromRN(fileName) { let json = {}; let received =
false; for (let i = 0; i < this.timeout; i++) { sleep.msleep(100); const fileContent = fs.readFileSync(fileName, 'utf8'); if (fileContent) { json = JSON.parse(fileContent); delete this.que[Number(json.queId)]; received = true; break; } sleep.msleep(900); } fs.unlinkSync(fileName); if (!received) { throw new Error("can't receive response from ReactNative Application"); } if (json.error === 1) { throw new Error(json.message); } return json.result; }
そうは言っても結構便利です。 ReactNative のお供にぜひ!
ありがとうございました