From a8b94ae65f0f4543e21bdc159e69d8e85b852081 Mon Sep 17 00:00:00 2001 From: nezuo Date: Mon, 23 Oct 2023 15:04:14 -0700 Subject: [PATCH] Add types --- src/PromiseTypes.lua | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/init.lua | 43 ++++++++++++++++++++++++++++++++----- 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/PromiseTypes.lua diff --git a/src/PromiseTypes.lua b/src/PromiseTypes.lua new file mode 100644 index 0000000..e4ef435 --- /dev/null +++ b/src/PromiseTypes.lua @@ -0,0 +1,51 @@ +export type Status = "Started" | "Resolved" | "Rejected" | "Cancelled" + +export type Promise = { + andThen: ( + self: Promise, + successHandler: (...any) -> ...any, + failureHandler: ((...any) -> ...any)? + ) -> Promise, + andThenCall: (self: Promise, callback: (T...) -> ...any, T...) -> any, + andThenReturn: (self: Promise, ...any) -> Promise, + + await: (self: Promise) -> (boolean, ...any), + awaitStatus: (self: Promise) -> (Status, ...any), + + cancel: (self: Promise) -> (), + catch: (self: Promise, failureHandler: (...any) -> ...any) -> Promise, + expect: (self: Promise) -> ...any, + + finally: (self: Promise, finallyHandler: (status: Status) -> ...any) -> Promise, + finallyCall: (self: Promise, callback: (T...) -> ...any, T...) -> Promise, + finallyReturn: (self: Promise, ...any) -> Promise, + + getStatus: (self: Promise) -> Status, + now: (self: Promise, rejectionValue: any?) -> Promise, + tap: (self: Promise, tapHandler: (...any) -> ...any) -> Promise, + timeout: (self: Promise, seconds: number, rejectionValue: any?) -> Promise, +} + +export type TypedPromise = { + andThen: (self: Promise, successHandler: (T...) -> ...any, failureHandler: ((...any) -> ...any)?) -> Promise, + andThenCall: (self: Promise, callback: (T...) -> ...any, T...) -> Promise, + andThenReturn: (self: Promise, ...any) -> Promise, + + await: (self: Promise) -> (boolean, T...), + awaitStatus: (self: Promise) -> (Status, T...), + + cancel: (self: Promise) -> (), + catch: (self: Promise, failureHandler: (...any) -> ...any) -> Promise, + expect: (self: Promise) -> T..., + + finally: (self: Promise, finallyHandler: (status: Status) -> ...any) -> Promise, + finallyCall: (self: Promise, callback: (T...) -> ...any, T...) -> Promise, + finallyReturn: (self: Promise, ...any) -> Promise, + + getStatus: (self: Promise) -> Status, + now: (self: Promise, rejectionValue: any?) -> Promise, + tap: (self: Promise, tapHandler: (T...) -> ...any) -> Promise, + timeout: (self: Promise, seconds: number, rejectionValue: any?) -> Promise, +} + +return nil diff --git a/src/init.lua b/src/init.lua index 7b10f4a..be73f57 100644 --- a/src/init.lua +++ b/src/init.lua @@ -1,14 +1,47 @@ local Internal = require(script.Internal) +local PromiseTypes = require(script.PromiseTypes) local internal = Internal.new(true) +export type DataStoreService = { + GetDataStore: (name: string) -> GlobalDataStore, + GetRequestBudgetForRequestType: (requestType: Enum.DataStoreRequestType) -> number, +} + +export type PartialLapisConfig = { + saveAttempts: number?, + loadAttempts: number?, + loadRetryDelay: number?, + showRetryWarnings: boolean?, + dataStoreService: DataStoreService?, + [any]: nil, +} + +export type CollectionOptions = { + defaultData: T, + migrations: { (any) -> any }?, + validate: (T) -> (boolean, string?), + [any]: nil, +} + +export type Collection = { + load: (self: Collection, key: string) -> PromiseTypes.TypedPromise>, +} + +export type Document = { + read: (self: Document) -> T, + write: (self: Document, T) -> (), + save: (self: Document) -> PromiseTypes.TypedPromise<()>, + close: (self: Document) -> PromiseTypes.TypedPromise<()>, +} + --[=[ @class Lapis ]=] local Lapis = {} --[=[ - @interface ConfigValues + @interface PartialLapisConfig @within Lapis .saveAttempts number? -- Max save/close retry attempts .loadAttempts number? -- Max load retry attempts @@ -36,10 +69,10 @@ local Lapis = {} } ``` - @param values ConfigValues + @param partialConfig PartialLapisConfig ]=] -function Lapis.setConfig(values) - internal.setConfig(values) +function Lapis.setConfig(partialConfig: PartialLapisConfig) + internal.setConfig(partialConfig) end --[=[ @@ -57,7 +90,7 @@ end @param options CollectionOptions @return Collection ]=] -function Lapis.createCollection(name, options) +function Lapis.createCollection(name: string, options: CollectionOptions): Collection return internal.createCollection(name, options) end