Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
AsyncStorageをNodeのREPLから操作するためにTop-levelでawaitし...
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
140
Building Public API with GraphQL
joere
3
150
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
380
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
2027年のMetricKit
kantacky
0
140
AI for Science時代を切り開く、政府の次世代HPC戦略の展望
gpuunite_official
0
170
:syncing_time:
sksat
2
290
三人寄ればチューリング完全
puhitaku
5
2.4k
MulticaとPi Coding Agentで、小規模OSSを30本同時運用した流れ
eiei114
0
140
DatadogのBits Chatが開発組織にもたらしたもの / What Bits Chat Has Brought Us
sms_tech
1
290
20260817_生成AIの動向と中学校・高等学校での活用を考える_v1.00_公開用
doradora09
PRO
0
180
ホームラボ紹介
y_sera15
0
220
AI開発に用いられるHPC技術について
gpuunite_official
0
230
ハッカソンで入賞した話 @ Findy LT
asari194617
0
1.4k
国家プロジェクトを支える「さくらONE」 大規模LLM開発におけるGPU障害を乗り越えるクラスター運用戦略
gpuunite_official
0
220
도구에서 동료까지: 10년차 AI 스타트업의 AI 적응기
inureyes
PRO
1
320
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Crafting Experiences
bethany
1
260
Mind Mapping
helmedeiros
PRO
1
330
Product Roadmaps are Hard
iamctodd
55
12k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
440
How to make the Groovebox
asonas
2
2.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
250
RailsConf 2023
tenderlove
30
1.5k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
460
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
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 のお供にぜひ!
ありがとうございました