diff --git a/web/bsconfig.json b/web/bsconfig.json index 5773a72..0848312 100644 --- a/web/bsconfig.json +++ b/web/bsconfig.json @@ -7,7 +7,7 @@ "bs-webapi", "bs-fetch", "@glennsl/bs-json", - "@aantron/repromise" + "reason-promise" ], "reason": { "react-jsx": 2 diff --git a/web/bsconfig.strict.json b/web/bsconfig.strict.json index bb0f476..967ce77 100644 --- a/web/bsconfig.strict.json +++ b/web/bsconfig.strict.json @@ -7,7 +7,7 @@ "bs-webapi", "bs-fetch", "@glennsl/bs-json", - "@aantron/repromise" + "reason-promise" ], "reason": { "react-jsx": 2 diff --git a/web/package.json b/web/package.json index bcbe03a..45f70da 100644 --- a/web/package.json +++ b/web/package.json @@ -3,10 +3,9 @@ "version": "0.1.0", "private": true, "dependencies": { - "@aantron/repromise": "^0.6.0", "@glennsl/bs-json": "^3.0.0", "bs-fetch": "^0.3.0", - "bs-platform": "^4.0.5", + "bs-platform": "^7.0.1", "bs-webapi": "^0.10.0", "classnames": "^2.2.6", "codemirror": "^5.40.2", @@ -24,6 +23,7 @@ "react": "^16.4.1", "react-codemirror2": "^5.1.0", "react-dom": "^16.4.1", + "reason-promise": "^1.0.2", "reason-react": "^0.5.3" }, "scripts": { diff --git a/web/src/Api.re b/web/src/Api.re index 696d659..ad3b081 100644 --- a/web/src/Api.re +++ b/web/src/Api.re @@ -157,7 +157,7 @@ let headers = () => { let toResult = (mapper: Fetch.Response.t => Js.Promise.t('b), promise) - : Repromise.t(Belt.Result.t('b, Js.Promise.error)) => + : Promise.t(Belt.Result.t('b, Js.Promise.error)) => promise |> Js.Promise.then_(response => if (!Fetch.Response.ok(response)) { @@ -169,20 +169,21 @@ let toResult = } ) |> Js.Promise.then_(mapper) - |> Promises.toResultPromise; + |> Promise.Js.fromBsPromise + |> Promise.Js.toResult; let toJsonResult = (mapper: Js.Json.t => 'a, promise) => - promise |> toResult(Fetch.Response.json) |> Promises.mapOk(mapper); + (promise |> toResult(Fetch.Response.json))->Promise.mapOk(mapper); let fetchChanges = - (revision: option(string)): Repromise.t(Belt.Result.t(apiResponse, Js.Promise.error)) => + (revision: option(string)): Promise.t(Belt.Result.t(apiResponse, Js.Promise.error)) => Fetch.fetchWithInit( fetchUrl(revision), Fetch.RequestInit.make(~method_=Get, ~headers=Fetch.HeadersInit.make(headers()), ()), ) |> toJsonResult(JsonCoders.decodeChangesResponse); -let createNote = (note: Data.note): Repromise.t(Belt.Result.t(Data.note, Js.Promise.error)) => { +let createNote = (note: Data.note): Promise.t(Belt.Result.t(Data.note, Js.Promise.error)) => { let json = JsonCoders.encodeNote(note); Fetch.fetchWithInit( @@ -228,7 +229,7 @@ let updateNotebook = (notebook: Data.notebook) => { }; let deleteNotebook = - (notebookId: string): Repromise.t(Belt.Result.t(Fetch.Response.t, Js.Promise.error)) => + (notebookId: string): Promise.t(Belt.Result.t(Fetch.Response.t, Js.Promise.error)) => Fetch.fetchWithInit( "/api/notebooks/" ++ notebookId, Fetch.RequestInit.make(~method_=Delete, ~headers=Fetch.HeadersInit.make(headers()), ()), diff --git a/web/src/Auth.re b/web/src/Auth.re index 32bc02d..8c48514 100644 --- a/web/src/Auth.re +++ b/web/src/Auth.re @@ -2,7 +2,7 @@ let getToken = () => LocalStorage.getItem("pragma-token"); let checkToken = (checkFn, token) => checkFn(token) - |> Promises.mapOk(result => { + ->Promise.mapOk(result => { LocalStorage.setItem("pragma-token", token); result; }); diff --git a/web/src/ContentBlocks.re b/web/src/ContentBlocks.re index 143cf39..b9bad29 100644 --- a/web/src/ContentBlocks.re +++ b/web/src/ContentBlocks.re @@ -6,7 +6,7 @@ let update = (block, ~sync=true, ()) => { let now = Js.Date.fromFloat(Js.Date.now()); Db.updateContentBlock({...block, updatedAt: now}, ~sync, ()) - |> Promises.tapOk(_ => + ->Promise.tapOk(_ => if (sync) { Db.touchNote(block.noteId) |> ignore; } @@ -37,5 +37,5 @@ let updateCodeLanguage = (block: Data.contentBlock, language) => let delete = id => Db.deleteContentBlock(id); DataSync.setContentBlockSyncedListener(block => - Db.updateContentBlock(block, ~sync=false, ()) |> Repromise.map(_ => ()) + Db.updateContentBlock(block, ~sync=false, ())->Promise.map(_ => ()) ); /* FIXME: error handling? */ diff --git a/web/src/DataSync.re b/web/src/DataSync.re index 6fedd98..d486bcf 100644 --- a/web/src/DataSync.re +++ b/web/src/DataSync.re @@ -16,7 +16,7 @@ type change = { let pendingChanges: ref(list(change)) = ref([]); let retryQueue: ref(list(change)) = ref([]); -type syncedListener('a) = 'a => Repromise.t(unit); +type syncedListener('a) = 'a => Promise.t(unit); let noteSyncedListener: ref(option(syncedListener(Data.note))) = ref(None); let notebookSyncedListener: ref(option(syncedListener(Data.notebook))) = ref(None); @@ -31,11 +31,11 @@ let setContentBlockSyncedListener = listener => contentBlockSyncedListener := So let notifyListener = (listener, resource) => ( switch (listener^) { - | None => Repromise.resolved() + | None => Promise.resolved() | Some(listener) => listener(resource) } ) - |> Repromise.map(v => Belt.Result.Ok(v)); + ->Promise.map(v => Belt.Result.Ok(v)); let notifyNoteSyncedListener = notifyListener(noteSyncedListener); let notifyNotebookSyncedListener = notifyListener(notebookSyncedListener); @@ -62,18 +62,18 @@ let notifyPendingChangesListener = pendingChanges => { let syncChange = change => switch (change.change) { - | NoteCreated(note) => Api.createNote(note) |> Promises.flatMapOk(notifyNoteSyncedListener) - | NoteUpdated(note) => Api.updateNote(note) |> Promises.flatMapOk(notifyNoteSyncedListener) + | NoteCreated(note) => Api.createNote(note)->Promise.flatMapOk(notifyNoteSyncedListener) + | NoteUpdated(note) => Api.updateNote(note)->Promise.flatMapOk(notifyNoteSyncedListener) | ContentBlockCreated(contentBlock) => - Api.createContentBlock(contentBlock) |> Promises.flatMapOk(notifyContentBlockSyncedListener) + Api.createContentBlock(contentBlock)->Promise.flatMapOk(notifyContentBlockSyncedListener) | ContentBlockUpdated(contentBlock) => - Api.updateContentBlock(contentBlock) |> Promises.flatMapOk(notifyContentBlockSyncedListener) + Api.updateContentBlock(contentBlock)->Promise.flatMapOk(notifyContentBlockSyncedListener) | NotebookCreated(notebook) => - Api.createNotebook(notebook) |> Promises.flatMapOk(notifyNotebookSyncedListener) + Api.createNotebook(notebook)->Promise.flatMapOk(notifyNotebookSyncedListener) | NotebookUpdated(notebook) => - Api.updateNotebook(notebook) |> Promises.flatMapOk(notifyNotebookSyncedListener) - | NotebookDeleted(notebookId) => Api.deleteNotebook(notebookId) |> Promises.mapOk(ignore) - | NoteDeleted(noteId) => Api.deleteNote(noteId) |> Promises.mapOk(ignore) + Api.updateNotebook(notebook)->Promise.flatMapOk(notifyNotebookSyncedListener) + | NotebookDeleted(notebookId) => Api.deleteNotebook(notebookId)->Promise.mapOk(ignore) + | NoteDeleted(noteId) => Api.deleteNote(noteId)->Promise.mapOk(ignore) }; let storePendingChanges = () => { @@ -163,7 +163,7 @@ let rec syncPendingChanges = onComplete => { switch (nextChange) { | Some([change]) => syncChange(change) - |> Repromise.wait(result => { + ->Promise.get(result => { if (Belt.Result.isError(result)) { Js.Console.error2("Error syncing: ", result); pushChangeToQueue(retryQueue, change); diff --git a/web/src/DataSyncRetry.re b/web/src/DataSyncRetry.re index fcfdc60..b9974a5 100644 --- a/web/src/DataSyncRetry.re +++ b/web/src/DataSyncRetry.re @@ -14,36 +14,36 @@ let getPendingChanges = () => { let change = switch (Js.String.split(":", changeId)) { | [|"contentBlock", "updated", id|] => - ContentBlocks.get(id) |> Promises.mapSome(cb => DataSync.ContentBlockUpdated(cb)) + ContentBlocks.get(id)->Promise.mapSome(cb => DataSync.ContentBlockUpdated(cb)) | [|"contentBlock", "created", id|] => - ContentBlocks.get(id) |> Promises.mapSome(cb => DataSync.ContentBlockCreated(cb)) + ContentBlocks.get(id)->Promise.mapSome(cb => DataSync.ContentBlockCreated(cb)) | [|"note", "created", id|] => - Notes.get(id) |> Promises.mapSome(note => DataSync.NoteCreated(note)) + Notes.get(id)->Promise.mapSome(note => DataSync.NoteCreated(note)) | [|"note", "updated", id|] => - Notes.get(id) |> Promises.mapSome(note => DataSync.NoteUpdated(note)) + Notes.get(id)->Promise.mapSome(note => DataSync.NoteUpdated(note)) - | [|"note", "deleted", id|] => Repromise.resolved(Some(DataSync.NoteDeleted(id))) + | [|"note", "deleted", id|] => Promise.resolved(Some(DataSync.NoteDeleted(id))) | [|"notebook", "created", id|] => - Notebooks.get(id) |> Promises.mapSome(notebook => DataSync.NotebookCreated(notebook)) + Notebooks.get(id)->Promise.mapSome(notebook => DataSync.NotebookCreated(notebook)) | [|"notebook", "updated", id|] => - Notebooks.get(id) |> Promises.mapSome(notebook => DataSync.NotebookUpdated(notebook)) + Notebooks.get(id)->Promise.mapSome(notebook => DataSync.NotebookUpdated(notebook)) | [|"notebook", "deleted", id|] => - Repromise.resolved(Some(DataSync.NotebookDeleted(id))) + Promise.resolved(Some(DataSync.NotebookDeleted(id))) | _ => Js.Console.error2("Unknown change id:", changeId); - Repromise.resolved(None); + Promise.resolved(None); }; - Promises.mapSome(change => {DataSync.id: changeId, change}, change); + Promise.mapSome(change, change => {DataSync.id: changeId, change}); }, ); - promises |> Repromise.all |> Repromise.map(result => Belt.List.keepMap(result, item => item)); + promises->Promise.all->Promise.map(result => Belt.List.keepMap(result, item => item)); }; diff --git a/web/src/Db.re b/web/src/Db.re index 6c15f28..8a9ca75 100644 --- a/web/src/Db.re +++ b/web/src/Db.re @@ -130,37 +130,40 @@ let initDb = () => { DbMigrations.runMigrations(upgradeDb, oldVersion, currentVersion); }, ) - |> Promises.toResultPromise; + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult; }; let dbPromise = () => switch (iDb^) { | None => initDb() - |> Repromise.map(result => { + ->Promise.map(result => { let db = Belt.Result.getExn(result); iDb := Some(db); db; }) - | Some(db) => Repromise.resolved(db) + | Some(db) => Promise.resolved(db) }; let toOptionPromise = (jsPromise: Js.Promise.t(option('a))) => jsPromise - |> Repromise.Rejectable.fromJsPromise - |> Repromise.Rejectable.map( + ->Promise.Js.fromBsPromise + ->Promise.Js.map( fun | None => None | Some(value) => Some(value), ) - |> Repromise.Rejectable.catch(error => { + ->Promise.Js.catch(error => { Js.Console.error2("Db promise failed: ", error); - Repromise.resolved(None); + Promise.resolved(None); }); let awaitTransactionPromise = (transaction: IndexedDB.Transaction.t) => - IndexedDB.Transaction.complete(transaction)->Promises.toResultPromise; + IndexedDB.Transaction.complete(transaction) + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult; let listeners: ref(list(listener)) = ref([]); @@ -170,14 +173,15 @@ let unsubscribe = listener => listeners := Belt.List.keep(listeners^, l => l !== let getNotes = (notebookId: string) => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, notesStore, Transaction.ReadOnly) ->Transaction.objectStore(notesStore) ->ObjectStore.index("forNotebook") ->IndexedDB.Index.getAllByKey(notebookId) - ->Promises.toResultPromise - |> Repromise.map( + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.map( fun | Ok(array) => array->Belt.List.fromArray->Belt.List.map(JsonCoders.decodeNote) | Error(_) => [], @@ -187,15 +191,16 @@ let getNotes = (notebookId: string) => let getRecentNotes = limit => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( - DB.transaction(db, notesStore, Transaction.ReadOnly) + (DB.transaction(db, notesStore, Transaction.ReadOnly) ->Transaction.objectStore(notesStore) ->ObjectStore.index("byUpdatedAt") ->IndexedDB.Index.openCursor(Cursor.Prev) - |> Js.Promise.then_(Cursor.take(_, limit, [||])) - |> Promises.toResultPromise - |> Repromise.map( + |> Js.Promise.then_(Cursor.take(_, limit, [||]))) + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.map( fun | Ok(array) => array->Belt.List.fromArray->Belt.List.map(JsonCoders.decodeNote) | Error(_) => [], @@ -205,85 +210,89 @@ let getRecentNotes = limit => let getRecentNotesCount = () => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, notesStore, Transaction.ReadOnly) ->Transaction.objectStore(notesStore) ->ObjectStore.index("byUpdatedAt") ->IndexedDB.Index.count - ->Promises.toResultPromise - |> Repromise.map(Belt.Result.getWithDefault(_, 0)) + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.map(Belt.Result.getWithDefault(_, 0)) ) ); let countNotes = (notebookId: string) => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, notesStore, Transaction.ReadOnly) ->Transaction.objectStore(notesStore) ->ObjectStore.index("forNotebook") ->IndexedDB.Index.countByKey(notebookId) - ->Promises.toResultPromise - |> Repromise.map(Belt.Result.getWithDefault(_, 0)) + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.map(Belt.Result.getWithDefault(_, 0)) ) ); let getNote = (noteId: string) => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, notesStore, Transaction.ReadOnly) ->Transaction.objectStore(notesStore) ->ObjectStore.get(noteId) ->toOptionPromise - |> Repromise.map(v => Belt.Option.map(v, JsonCoders.decodeNote)) + ->Promise.map(v => Belt.Option.map(v, JsonCoders.decodeNote)) ) ); let getNotebooks = () => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, notebooksStore, Transaction.ReadOnly) ->Transaction.objectStore(notebooksStore) ->ObjectStore.getAll - ->Promises.toResultPromise - |> Repromise.andThen( + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.flatMap( fun | Ok(array) => array ->Belt.List.fromArray ->Belt.List.map(JsonCoders.decodeNotebook) - ->Belt.List.map(n => countNotes(n.id) |> Repromise.map(count => (n, count))) - ->Repromise.all - | Error(_) => Repromise.resolved([]), + ->Belt.List.map(n => countNotes(n.id)->Promise.map(count => (n, count))) + ->Promise.all + | Error(_) => Promise.resolved([]), ) ) ); let getNotebook = (notebookId: string) => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, notebooksStore, Transaction.ReadOnly) ->Transaction.objectStore(notebooksStore) ->ObjectStore.get(notebookId) ->toOptionPromise - |> Repromise.map(v => Belt.Option.map(v, JsonCoders.decodeNotebook)) + ->Promise.map(v => Belt.Option.map(v, JsonCoders.decodeNotebook)) ) ); let getContentBlocks = noteId => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, contentBlocksStore, Transaction.ReadOnly) ->Transaction.objectStore(contentBlocksStore) ->ObjectStore.index("forNote") ->IndexedDB.Index.getAllByKey(noteId) - ->Promises.toResultPromise - |> Repromise.map( + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.map( fun | Ok(array) => array->Belt.List.fromArray->Belt.List.map(JsonCoders.decodeContentBlock) @@ -294,20 +303,20 @@ let getContentBlocks = noteId => let getContentBlock = blockId => dbPromise() - |> Repromise.andThen(db => + ->Promise.flatMap(db => IndexedDB.( DB.transaction(db, contentBlocksStore, Transaction.ReadOnly) ->Transaction.objectStore(contentBlocksStore) ->ObjectStore.get(blockId) ->toOptionPromise - |> Repromise.map(v => Belt.Option.map(v, JsonCoders.decodeContentBlock)) + ->Promise.map(v => Belt.Option.map(v, JsonCoders.decodeContentBlock)) ) ); let addNotebook = notebook => IndexedDB.( dbPromise() - |> Repromise.andThen(db => { + ->Promise.flatMap(db => { let tx = DB.transaction(db, notebooksStore, Transaction.ReadWrite); let data = JsonCoders.encodeNotebook(notebook); @@ -320,7 +329,7 @@ let addNotebook = notebook => let addNote = note => IndexedDB.( dbPromise() - |> Repromise.andThen(db => { + ->Promise.flatMap(db => { let tx = DB.transaction(db, notesStore, Transaction.ReadWrite); let data = JsonCoders.encodeNote(note); @@ -333,7 +342,7 @@ let addNote = note => let addContentBlock = block => IndexedDB.( dbPromise() - |> Repromise.andThen(db => { + ->Promise.flatMap(db => { let tx = DB.transaction(db, contentBlocksStore, Transaction.ReadWrite); let data = JsonCoders.encodeContentBlock(block); @@ -365,8 +374,8 @@ let createNote = (notebookId: string) => { revision: None, }; - Repromise.all([addNote(note), addContentBlock(contentBlock)]) - |> Repromise.map(_ => { + Promise.all([addNote(note), addContentBlock(contentBlock)]) + ->Promise.map(_ => { /* FIXME: only push when succesful */ DataSync.pushNewNote(note); DataSync.pushNewContentBlock(contentBlock); @@ -377,7 +386,7 @@ let createNote = (notebookId: string) => { let createNotebook = notebook => addNotebook(notebook) - |> Repromise.map(_result => { + ->Promise.map(_result => { /* FIXME: only push when create is succesfull */ DataSync.pushNewNotebook(notebook) |> ignore; Ok(notebook); @@ -385,7 +394,7 @@ let createNotebook = notebook => let updateContentBlock = (contentBlock: Data.contentBlock, ~sync=true, ()) => addContentBlock(contentBlock) - |> Repromise.map(result => { + ->Promise.map(result => { /* FIXME: only push when create is succesfull */ if (sync) { DataSync.pushContentBlock(contentBlock); @@ -396,7 +405,7 @@ let updateContentBlock = (contentBlock: Data.contentBlock, ~sync=true, ()) => let updateNote = (note: Data.note, ~sync=true, ()) => addNote(note) - |> Repromise.map(result => { + ->Promise.map(result => { /* FIXME: only push when create is succesfull */ if (sync) { DataSync.pushNoteChange(note); @@ -407,7 +416,7 @@ let updateNote = (note: Data.note, ~sync=true, ()) => let updateNotebook = (notebook: Data.notebook, ~sync=true, ()) => addNotebook(notebook) - |> Repromise.map(result => { + ->Promise.map(result => { /* FIXME: only push when create is succesfull */ if (sync) { DataSync.pushNotebookChange(notebook); @@ -418,15 +427,16 @@ let updateNotebook = (notebook: Data.notebook, ~sync=true, ()) => let deleteNotebook = (notebookId: string, ~sync=true, ()) => dbPromise() - |> Repromise.andThen(db => { + ->Promise.flatMap(db => { open IndexedDB; let tx = DB.transaction(db, notebooksStore, Transaction.ReadWrite); Transaction.objectStore(tx, notebooksStore) ->ObjectStore.delete(notebookId) - ->Promises.toResultPromise; + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult }) - |> Repromise.map(_result => { + ->Promise.map(_result => { /* FIXME: only push when succesfull */ if (sync) { DataSync.pushNotebookDelete(notebookId); @@ -437,15 +447,16 @@ let deleteNotebook = (notebookId: string, ~sync=true, ()) => let deleteNote = (noteId: string, ~sync=true, ()) => dbPromise() - |> Repromise.andThen(db => { + ->Promise.flatMap(db => { open IndexedDB; let tx = DB.transaction(db, notesStore, Transaction.ReadWrite); Transaction.objectStore(tx, notesStore) ->ObjectStore.delete(noteId) - ->Promises.toResultPromise; + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult }) - |> Repromise.map(_result => { + ->Promise.map(_result => { /* FIXME: only push when succesfull */ if (sync) { DataSync.pushNoteDelete(noteId); @@ -456,22 +467,23 @@ let deleteNote = (noteId: string, ~sync=true, ()) => let deleteContentBlock = (contentBlockId: string) => dbPromise() - |> Repromise.andThen(db => { + ->Promise.flatMap(db => { open IndexedDB; let tx = DB.transaction(db, contentBlocksStore, Transaction.ReadWrite); Transaction.objectStore(tx, contentBlocksStore) ->ObjectStore.delete(contentBlockId) - ->Promises.toResultPromise; + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult }) - |> Repromise.map(_result + ->Promise.map(_result /* FIXME: only push when succesfull */ => Ok()); let insertRevision = (revision: string) => - LocalStorage.setItem("pragma-revision", revision)->Repromise.resolved; + LocalStorage.setItem("pragma-revision", revision)->Promise.resolved; -let getRevision = () => LocalStorage.getItem("pragma-revision")->Repromise.resolved; +let getRevision = () => LocalStorage.getItem("pragma-revision")->Promise.resolved; let withNotification = fn => { let result = fn(); @@ -481,7 +493,7 @@ let withNotification = fn => { }; let withPromiseNotification = promise => - promise |> Repromise.wait(_ => Belt.List.forEach(listeners^, l => l())); + promise->Promise.get(_ => Belt.List.forEach(listeners^, l => l())); let clear = () => { IndexedDB.delete("pragma") |> ignore; @@ -492,11 +504,11 @@ let touchNote = noteId => { let now = Js.Date.fromFloat(Js.Date.now()); getNote(noteId) - |> Promises.mapSome((note: Data.note) => {...note, updatedAt: now}) - |> Repromise.andThen(maybeNote => + ->Promise.mapSome((note: Data.note) => {...note, updatedAt: now}) + ->Promise.flatMap(maybeNote => switch (maybeNote) { - | None => Repromise.resolved(Belt.Result.Error()) - | Some(note) => updateNote(note, ()) |> Repromise.map(Results.mapError(_, _ => ())) + | None => Promise.resolved(Belt.Result.Error()) + | Some(note) => updateNote(note, ())->Promise.map(Results.mapError(_, _ => ())) } ); }; diff --git a/web/src/Db.rei b/web/src/Db.rei index b2b6e00..599dfea 100644 --- a/web/src/Db.rei +++ b/web/src/Db.rei @@ -4,38 +4,38 @@ type listener = unit => unit; let subscribe: listener => unit; let unsubscribe: listener => unit; -let addNotebook: Data.notebook => Repromise.t(result(unit)); -let addNote: Data.note => Repromise.t(result(unit)); +let addNotebook: Data.notebook => Promise.t(result(unit)); +let addNote: Data.note => Promise.t(result(unit)); -let createNote: string => Repromise.t(result((Data.note, Data.contentBlock))); -let createNotebook: Data.notebook => Repromise.t(result(Data.notebook)); +let createNote: string => Promise.t(result((Data.note, Data.contentBlock))); +let createNotebook: Data.notebook => Promise.t(result(Data.notebook)); -let getNote: string => Repromise.t(option(Data.note)); -let getNotes: string => Repromise.t(list(Data.note)); +let getNote: string => Promise.t(option(Data.note)); +let getNotes: string => Promise.t(list(Data.note)); -let getRecentNotes: (int) => Repromise.t(list(Data.note)); -let getRecentNotesCount: unit => Repromise.t(int); +let getRecentNotes: (int) => Promise.t(list(Data.note)); +let getRecentNotesCount: unit => Promise.t(int); -let getNotebooks: unit => Repromise.t(list((Data.notebook, int))); -let getNotebook: string => Repromise.t(option(Data.notebook)); +let getNotebooks: unit => Promise.t(list((Data.notebook, int))); +let getNotebook: string => Promise.t(option(Data.notebook)); -let getContentBlocks: string => Repromise.t(list(Data.contentBlock)); -let getContentBlock: string => Repromise.t(option(Data.contentBlock)); -let addContentBlock: Data.contentBlock => Repromise.t(result(unit)); +let getContentBlocks: string => Promise.t(list(Data.contentBlock)); +let getContentBlock: string => Promise.t(option(Data.contentBlock)); +let addContentBlock: Data.contentBlock => Promise.t(result(unit)); -let updateContentBlock: (Data.contentBlock, ~sync: bool=?, unit) => Repromise.t(result(unit)); -let updateNote: (Data.note, ~sync:bool=?, unit) => Repromise.t(result(unit)); -let updateNotebook: (Data.notebook, ~sync:bool=?, unit) => Repromise.t(result(unit)); +let updateContentBlock: (Data.contentBlock, ~sync: bool=?, unit) => Promise.t(result(unit)); +let updateNote: (Data.note, ~sync:bool=?, unit) => Promise.t(result(unit)); +let updateNotebook: (Data.notebook, ~sync:bool=?, unit) => Promise.t(result(unit)); -let touchNote: string => Repromise.t(Belt.Result.t(unit, unit)); +let touchNote: string => Promise.t(Belt.Result.t(unit, unit)); -let deleteNotebook: (string, ~sync: bool=?, unit) => Repromise.t(result(unit)); -let deleteNote: (string, ~sync: bool=?, unit) => Repromise.t(result(unit)); -let deleteContentBlock: (string) => Repromise.t(result(unit)); +let deleteNotebook: (string, ~sync: bool=?, unit) => Promise.t(result(unit)); +let deleteNote: (string, ~sync: bool=?, unit) => Promise.t(result(unit)); +let deleteContentBlock: (string) => Promise.t(result(unit)); let withNotification: (unit => 'a) => 'a; -let withPromiseNotification: Repromise.t('a) => unit; +let withPromiseNotification: Promise.t('a) => unit; -let insertRevision: string => Repromise.t(unit); -let getRevision: unit => Repromise.t(option(string)); +let insertRevision: string => Promise.t(unit); +let getRevision: unit => Promise.t(option(string)); let clear: unit => unit; diff --git a/web/src/DbSync.re b/web/src/DbSync.re index 4d02107..acc0ea4 100644 --- a/web/src/DbSync.re +++ b/web/src/DbSync.re @@ -2,7 +2,7 @@ open Belt; let upsertContentBlock = (contentBlock: Data.contentBlock) => ContentBlocks.get(contentBlock.id) - |> Repromise.andThen(storedContentBlock => + ->Promise.flatMap(storedContentBlock => switch (storedContentBlock) { | None => ContentBlocks.add(contentBlock) | Some(_contentBlock) => ContentBlocks.update(contentBlock, ~sync=false, ()) @@ -11,7 +11,7 @@ let upsertContentBlock = (contentBlock: Data.contentBlock) => let upsertNote = (note: Data.note) => Notes.get(note.id) - |> Repromise.andThen(storedNote => + ->Promise.flatMap(storedNote => switch (storedNote) { | None => Notes.add(note) | Some(_note) => Notes.update(note, ~sync=false, ()) @@ -20,7 +20,7 @@ let upsertNote = (note: Data.note) => let upsertNotebook = (notebook: Data.notebook) => Notebooks.get(notebook.id) - |> Repromise.andThen(storedNotebook => + ->Promise.flatMap(storedNotebook => switch (storedNotebook) { | None => Notebooks.add(notebook) | Some(_note) => Notebooks.update(notebook, ~sync=false, ()) @@ -29,8 +29,8 @@ let upsertNotebook = (notebook: Data.notebook) => let run = () => Db.getRevision() - |> Repromise.andThen(Api.fetchChanges) - |> Repromise.wait((result: Belt.Result.t(Api.apiResponse, _)) => + ->Promise.flatMap(Api.fetchChanges) + ->Promise.get((result: Belt.Result.t(Api.apiResponse, _)) => switch (result) { | Result.Ok(result) => let revision = result.revision; @@ -51,7 +51,7 @@ let run = () => ); let promise = - Repromise.all( + Promise.all( List.concatMany([| notebookResults, noteResults, diff --git a/web/src/Notebooks.re b/web/src/Notebooks.re index df35188..334291a 100644 --- a/web/src/Notebooks.re +++ b/web/src/Notebooks.re @@ -2,7 +2,7 @@ let all = () => Db.getNotebooks(); let get = id => Db.getNotebook(id); let add = notebook => Db.addNotebook(notebook); -let create = notebook => Db.createNotebook(notebook) |> Promises.tapOk(DataSync.pushNewNotebook); +let create = notebook => Db.createNotebook(notebook)->Promise.tapOk(DataSync.pushNewNotebook); let update = (notebook: Data.notebook, ~sync=true, ()) => { let now = Js.Date.fromFloat(Js.Date.now()); @@ -12,5 +12,5 @@ let update = (notebook: Data.notebook, ~sync=true, ()) => { let delete = (id, ~sync=true, ()) => Db.deleteNotebook(id, ~sync, ()); DataSync.setNotebookSyncedListener(notebook => - update(notebook, ~sync=false, ()) |> Repromise.map(_ => ()) + update(notebook, ~sync=false, ())->Promise.map(_ => ()) ); /* FIXME: error handling? */ diff --git a/web/src/Notes.re b/web/src/Notes.re index 99d3b40..9fa5238 100644 --- a/web/src/Notes.re +++ b/web/src/Notes.re @@ -11,5 +11,5 @@ let update = (note: Data.note, ~sync=true, ()) => { let delete = (noteId: string, ~sync=true, ()) => Db.deleteNote(noteId, ~sync, ()); DataSync.setNoteSyncedListener(note => - Db.updateNote(note, ~sync=false, ()) |> Repromise.map(_ => ()) + Db.updateNote(note, ~sync=false, ())->Promise.map(_ => ()) ); /* FIXME: error handling? */ diff --git a/web/src/components/CodeEditor.re b/web/src/components/CodeEditor.re index 0e439c5..814201f 100644 --- a/web/src/components/CodeEditor.re +++ b/web/src/components/CodeEditor.re @@ -145,8 +145,9 @@ module CodeMirrorWrapper = { let mode = SupportedLanguageMap.getExn(supportedLanguages, language); mode.install() - |> Promises.toResultPromise - |> Promises.tapOk(_ => CodeMirror.Editor.setOption(editor, "mode", mapMode(language))); + ->Promise.Js.fromBsPromise + ->Promise.Js.toResult + ->Promise.tapOk(_ => CodeMirror.Editor.setOption(editor, "mode", mapMode(language))); }; let component = ReasonReact.reducerComponent("CodeMirrorWrapper"); diff --git a/web/src/containers/noteManagement/NoteManagementContainer.re b/web/src/containers/noteManagement/NoteManagementContainer.re index 5273107..66ac23f 100644 --- a/web/src/containers/noteManagement/NoteManagementContainer.re +++ b/web/src/containers/noteManagement/NoteManagementContainer.re @@ -79,17 +79,17 @@ let sortNotebooksDesc = (notebooks: list((Data.notebook, int))) => sortDesc(notebooks, ((notebook, _)) => notebook.updatedAt); let getSortedNotes = notebookId => - Notes.fromNotebook(notebookId) |> Repromise.map(sortNotesDesc); + Notes.fromNotebook(notebookId)->Promise.map(sortNotesDesc); let fetchAllData = () => { open Belt; let appState = AppState.get(); Notebooks.all() - |> Repromise.map(sortNotebooksDesc) - |> Repromise.andThen((notebooks: list((Data.notebook, int))) => + ->Promise.map(sortNotebooksDesc) + ->Promise.flatMap((notebooks: list((Data.notebook, int))) => Db.getRecentNotesCount() - |> Repromise.map(count => { + ->Promise.map(count => { let count = count > maxRecentNotes ? maxRecentNotes : count; let collection = NoteCollection.makeCollection(NoteCollection.CollectionKind.Recents, count); @@ -97,7 +97,7 @@ let fetchAllData = () => { (notebooks, [collection]); }) ) - |> Repromise.andThen(((notebooks: list((Data.notebook, int)), noteCollections)) => { + ->Promise.flatMap(((notebooks: list((Data.notebook, int)), noteCollections)) => { let firstNotebookId = List.head(notebooks)->Belt.Option.map(((notebook, _)) => notebook.id); let selectedCollectionId = Utils.Option.or_(appState.selectedNotebookId, firstNotebookId); @@ -107,14 +107,14 @@ let fetchAllData = () => { switch (selectedCollection) { | Some(NoteCollection.Notebook(notebook)) => getSortedNotes(notebook.id) - |> Repromise.map(notes => (notebooks, selectedCollectionId, notes, noteCollections)) + ->Promise.map(notes => (notebooks, selectedCollectionId, notes, noteCollections)) | Some(NoteCollection.Collection({kind: NoteCollection.CollectionKind.Recents})) => Db.getRecentNotes(maxRecentNotes) - |> Repromise.map(notes => (notebooks, selectedCollectionId, notes, noteCollections)) - | None => Repromise.resolved((notebooks, None, [], noteCollections)) + ->Promise.map(notes => (notebooks, selectedCollectionId, notes, noteCollections)) + | None => Promise.resolved((notebooks, None, [], noteCollections)) }; }) - |> Repromise.andThen( + ->Promise.flatMap( ((notebooksWithCounts, selectedCollection, notes: list(Data.note), noteCollections)) => { let selectedNoteId = switch (appState.selectedNoteId) { @@ -124,12 +124,12 @@ let fetchAllData = () => { let contentBlocksPromise = switch (selectedNoteId) { - | None => Repromise.resolved([]) + | None => Promise.resolved([]) | Some(noteId) => ContentBlocks.fromNote(noteId) }; contentBlocksPromise - |> Repromise.map(contentBlocks => + ->Promise.map(contentBlocks => { initialStateLoaded: true, notebooks: notebooksWithCounts, @@ -145,7 +145,7 @@ let fetchAllData = () => { let selectFirstNote = notes => notes - |> Repromise.map((notes: list(Data.note)) => { + ->Promise.map((notes: list(Data.note)) => { let selectedNoteId = Belt.List.head(notes)->Belt.Option.map(note => note.id); (notes, selectedNoteId); }); @@ -168,7 +168,7 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { switch (action) { | ReloadState => ReasonReact.SideEffects( - self => fetchAllData() |> Repromise.wait(state => self.send(LoadState(state))), + self => fetchAllData()->Promise.get(state => self.send(LoadState(state))), ) | LoadState(state) => ReasonReact.Update(state) | SelectNotebook( @@ -177,9 +177,9 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { ReasonReact.UpdateWithSideEffects( {...state, selectedCollection: Some(NoteCollection.Collection.id(collection))}, self => - Db.getRecentNotes(maxRecentNotes) - |> selectFirstNote - |> Repromise.wait(((notes, selectedNoteId)) => { + (Db.getRecentNotes(maxRecentNotes) + |> selectFirstNote) + ->Promise.get(((notes, selectedNoteId)) => { self.send(NotebookSelected(notes, selectedNoteId)); switch (selectedNoteId) { @@ -193,7 +193,7 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { {...state, selectedCollection: Some(notebook.id)}, self => getNotes(notebook.id) - |> Repromise.wait(((notes, selectedNoteId)) => { + ->Promise.get(((notes, selectedNoteId)) => { self.send(NotebookSelected(notes, selectedNoteId)); switch (selectedNoteId) { @@ -212,9 +212,9 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { newState, self => Db.withNotification(() => Notebooks.create(notebook)) - |> Promises.mapOk(NoteCollection.fromNotebook) - |> Promises.mapOk(collection => self.send(SelectNotebook(collection))) - |> ignore, + ->Promise.mapOk(NoteCollection.fromNotebook) + ->Promise.mapOk(collection => self.send(SelectNotebook(collection))) + ->ignore, ); | DeleteNotebook(notebook) => let updatedNotebooks = @@ -248,7 +248,7 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { {...state, selectedNote: Some(noteId)}, self => ContentBlocks.fromNote(noteId) - |> Repromise.wait(contentBlocks => self.send(NoteSelected(noteId, contentBlocks))), + ->Promise.get(contentBlocks => self.send(NoteSelected(noteId, contentBlocks))), ) | NoteSelected(noteId, contentBlocks) => ReasonReact.Update({...state, selectedNote: Some(noteId), contentBlocks}) @@ -256,12 +256,12 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { ReasonReact.SideEffects( self => Notes.create(self.state.selectedCollection |> Belt.Option.getExn) - |> Promises.tapOk(((note: Data.note, _contentBlock)) => { + ->Promise.tapOk(((note: Data.note, _contentBlock)) => { AppState.setSelected(self.state.selectedCollection, Some(note.id)); - fetchAllData() |> Repromise.wait(state => self.send(LoadState(state))); + fetchAllData()->Promise.get(state => self.send(LoadState(state))); }) - |> ignore, + ->ignore, ) | DeleteNote(note) => let updatedNotes = Belt.List.keep(state.notes, existingNote => existingNote.id != note.id); @@ -327,7 +327,7 @@ let make = (children: (state, action => unit) => ReasonReact.reactElement) => { }; serverSync(); - DataSyncRetry.getPendingChanges() |> Repromise.wait(DataSync.start); + DataSyncRetry.getPendingChanges()->Promise.get(DataSync.start); let loadStateFromDb = () => self.send(ReloadState); loadStateFromDb(); diff --git a/web/src/containers/noteManagement/NotebooksContainer.re b/web/src/containers/noteManagement/NotebooksContainer.re index 817e012..ad87942 100644 --- a/web/src/containers/noteManagement/NotebooksContainer.re +++ b/web/src/containers/noteManagement/NotebooksContainer.re @@ -21,7 +21,7 @@ let make = let promise = Notebooks.create(notebook) - |> Promises.tapOk(_ => { + ->Promise.tapOk(_ => { let collection = UiTypes.NoteCollection.fromNotebook(notebook); dispatch(NoteManagementContainer.SelectNotebook(collection)); send(EditTitle(Some(notebook.id))); diff --git a/web/src/containers/onboarding/LoginContainer.re b/web/src/containers/onboarding/LoginContainer.re index 5ff0fb1..87d5503 100644 --- a/web/src/containers/onboarding/LoginContainer.re +++ b/web/src/containers/onboarding/LoginContainer.re @@ -71,7 +71,7 @@ let make = (~onLoggedIn, _children) => { let password = passwordInputValue(self.state); Auth.checkToken(Api.checkAuth, password) - |> Repromise.wait(result => + ->Promise.get(result => switch (result) { | Belt.Result.Ok(_) => self.send(Proceed(AuthSuccesful)); diff --git a/web/src/support/Promises.re b/web/src/support/Promises.re deleted file mode 100644 index fccee91..0000000 --- a/web/src/support/Promises.re +++ /dev/null @@ -1,50 +0,0 @@ -open Belt.Result; - -let mapOk = - (mapper: 'a => 'c, promise: Repromise.t(Belt.Result.t('a, 'b))) - : Repromise.t(Belt.Result.t('c, 'b)) => - promise - |> Repromise.map( - fun - | Ok(value) => Ok(mapper(value)) - | Error(_) as error => error, - ); - -let tapOk = - (tap: 'a => unit, promise: Repromise.t(Belt.Result.t('a, 'b))) - : Repromise.t(Belt.Result.t('a, 'b)) => - mapOk( - value => { - tap(value); - value; - }, - promise, - ); - -let flatMapOk = - ( - mapper: 'a => Repromise.t(Belt.Result.t('c, 'b)), - promise: Repromise.t(Belt.Result.t('a, 'b)), - ) - : Repromise.t(Belt.Result.t('c, 'b)) => - promise - |> Repromise.andThen( - fun - | Ok(value) => mapper(value) - | Error(_) as error => Repromise.resolved(error), - ); - -let mapSome = (mapper: 'a => 'b, promise: Repromise.t(option('a))): Repromise.t(option('b)) => - promise - |> Repromise.map( - fun - | Some(value) => Some(mapper(value)) - | None => None, - ); - -let toResultPromise = - (promise: Js.Promise.t('a)): Repromise.t(Belt.Result.t('a, Js.Promise.error)) => - promise - |> Repromise.Rejectable.fromJsPromise - |> Repromise.Rejectable.map(value => Belt.Result.Ok(value)) - |> Repromise.Rejectable.catch(error => Repromise.resolved(Belt.Result.Error(error))); diff --git a/web/yarn.lock b/web/yarn.lock index acf2abd..b9d3fa7 100644 --- a/web/yarn.lock +++ b/web/yarn.lock @@ -2,11 +2,6 @@ # yarn lockfile v1 -"@aantron/repromise@^0.6.0": - version "0.6.0" - resolved "https://registry.yarnpkg.com/@aantron/repromise/-/repromise-0.6.0.tgz#45bea66e593b7e0f968bc0fd03f0b6578003e698" - integrity sha512-nxNUWKQQhRJKmsExKi2qyZUryvpEz07H17fEhRaZC3wXkxCtjcdx9j5LMnV0+anbU+EyG/X+UnfATReerBZgMA== - "@babel/code-frame@7.0.0-beta.46": version "7.0.0-beta.46" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.46.tgz#e0d002100805daab1461c0fcb32a07e304f3a4f4" @@ -1776,10 +1771,10 @@ bs-loader@^2.0.3: loader-utils "^1.1.0" read-bsconfig "^1.0.1" -bs-platform@^4.0.5: - version "4.0.18" - resolved "https://registry.yarnpkg.com/bs-platform/-/bs-platform-4.0.18.tgz#d17b8693ba1f714d9b27e0ddb365f2069ba4b8a0" - integrity sha512-BwzW0iYHvREqUZIgQxJmdJrxexppLvJxYQ4LLexbhCp7uZU5DIZ5ub4ZHpkCkc8fn8bsXWc+Rrejb3csi+BoAQ== +bs-platform@^7.0.1: + version "7.1.0" + resolved "https://registry.yarnpkg.com/bs-platform/-/bs-platform-7.1.0.tgz#72b52148b1c4be7f878969e6e2afd1bfab068cdd" + integrity sha512-XUeZf1nGzmsVymG89j5L8G9YNDHl0J/5iDGExXA7a4RKxnbvP2TselBZAzFeEH4rs3gG01b7yKt+h2pm7yh7Ww== bs-webapi@^0.10.0: version "0.10.0" @@ -7983,6 +7978,11 @@ realpath-native@^1.0.0: dependencies: util.promisify "^1.0.0" +reason-promise@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/reason-promise/-/reason-promise-1.0.2.tgz#22b9bb4097ce511190182b948aef11427d5b8b86" + integrity sha512-j8DWV+71wNEKQmyW6zBOowIZq1Qec5CDWyLI37BvgOmAZgRFGFQ1MaJnbhqDe3JsYmaGyqdNMmpCJLl9EDbDAg== + reason-react@^0.5.3: version "0.5.3" resolved "https://registry.yarnpkg.com/reason-react/-/reason-react-0.5.3.tgz#10601809742fd991109ec9d69ad4baf2c3f17540"