Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade @aantron/repromise to reason-promise #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bs-webapi",
"bs-fetch",
"@glennsl/bs-json",
"@aantron/repromise"
"reason-promise"
],
"reason": {
"react-jsx": 2
Expand Down
2 changes: 1 addition & 1 deletion web/bsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bs-webapi",
"bs-fetch",
"@glennsl/bs-json",
"@aantron/repromise"
"reason-promise"
],
"reason": {
"react-jsx": 2
Expand Down
4 changes: 2 additions & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
13 changes: 7 additions & 6 deletions web/src/Api.re
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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(
Expand Down Expand Up @@ -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()), ()),
Expand Down
2 changes: 1 addition & 1 deletion web/src/Auth.re
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
4 changes: 2 additions & 2 deletions web/src/ContentBlocks.re
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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? */
24 changes: 12 additions & 12 deletions web/src/DataSync.re
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions web/src/DataSyncRetry.re
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Loading