Skip to content

Commit

Permalink
Merge pull request #24 from nezuo/types
Browse files Browse the repository at this point in the history
Type public API
  • Loading branch information
nezuo authored Oct 23, 2023
2 parents a5f855e + a8b94ae commit 8e358ea
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
51 changes: 51 additions & 0 deletions src/PromiseTypes.lua
Original file line number Diff line number Diff line change
@@ -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: <T...>(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: <T...>(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<T...> = {
andThen: (self: Promise, successHandler: (T...) -> ...any, failureHandler: ((...any) -> ...any)?) -> Promise,
andThenCall: <T...>(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: <T...>(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
43 changes: 38 additions & 5 deletions src/init.lua
Original file line number Diff line number Diff line change
@@ -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<T> = {
defaultData: T,
migrations: { (any) -> any }?,
validate: (T) -> (boolean, string?),
[any]: nil,
}

export type Collection<T> = {
load: (self: Collection<T>, key: string) -> PromiseTypes.TypedPromise<Document<T>>,
}

export type Document<T> = {
read: (self: Document<T>) -> T,
write: (self: Document<T>, T) -> (),
save: (self: Document<T>) -> PromiseTypes.TypedPromise<()>,
close: (self: Document<T>) -> 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
Expand Down Expand Up @@ -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

--[=[
Expand All @@ -57,7 +90,7 @@ end
@param options CollectionOptions
@return Collection
]=]
function Lapis.createCollection(name, options)
function Lapis.createCollection<T>(name: string, options: CollectionOptions<T>): Collection<T>
return internal.createCollection(name, options)
end

Expand Down

0 comments on commit 8e358ea

Please sign in to comment.