Skip to content
This repository has been archived by the owner on Oct 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #24 from pairshaped/initialized-outside-elm
Browse files Browse the repository at this point in the history
Support firebase when initialized outside of elm
  • Loading branch information
mrozbarry authored Apr 26, 2017
2 parents 1ee7c53 + 24e0e15 commit e211703
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CREDITS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Credits

- [@tilmans](https://github.com/tilmans) - Early adopter, and guinea pig for all that code I should have tested and haven't yet.

- [@ucode](https://github.com/ucode) - Brought the new firebase environment initialization to my attention.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Then you can add elm-firebase to your elm-package.json like so:
```
{
"dependencies": {
"pairshaped/elm-firebase": "0.0.12 <= v < 1.0.0"
"pairshaped/elm-firebase": "0.0.13 <= v < 1.0.0"
},
"dependency-sources": {
"pairshaped/elm-firebase": {
"url": "https://github.com/pairshaped/elm-firebase",
"ref": "master"
"ref": "v0.0.13"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.12",
"version": "0.0.13",
"summary": "Firebase bindings for elm",
"repository": "https://github.com/pairshaped/elm-firebase.git",
"license": "BSD3",
Expand Down
11 changes: 11 additions & 0 deletions examples/kitchenSink/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type Msg
| SignInAnonymously
| SignedIn (Result Error User)
| SignOut
| GetApp
| NoOp ()


Expand Down Expand Up @@ -238,6 +239,13 @@ update msg model =
, Task.perform NoOp (Firebase.Authentication.signOut auth)
)

GetApp ->
let
_ =
Debug.log "Firebase app" (Firebase.app ())
in
update (NoOp ()) model

NoOp _ ->
( model
, Cmd.none
Expand Down Expand Up @@ -272,6 +280,9 @@ view model =
]
, div [] [ text ("Collection query = " ++ (toString model.collection)) ]
, viewSignIn model.currentUser
, button
[ onClick GetApp ]
[ text "Check console for current app" ]
]


Expand Down
31 changes: 30 additions & 1 deletion src/Firebase.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Firebase
, Config
, sdkVersion
, apps
, app
, init
, initWithName
, deinit
Expand All @@ -17,7 +18,7 @@ module Firebase
@docs App, Config
# App methods
@docs init, initWithName, deinit, name, options
@docs app, init, initWithName, deinit, name, options
# Helpers
@docs sdkVersion, apps
Expand Down Expand Up @@ -77,6 +78,34 @@ apps =
Native.Firebase.apps


{-| Get the currently initialized app if there is one
Maps to `firebase.app`
-}

app : () -> Maybe App
app =
Native.Firebase.app


{-| Find an app with a given name
To get the default app:
```
app : Maybe Firebase.App
app =
Firebase.getAppByName "[DEFAULT]"
```
Does not map to a Firebase method, it's just a convenience
-}
getAppByName : String -> Maybe App
getAppByName appName =
apps ()
|> List.filter (\app -> (name app) == appName)
|> List.head


-- App Methods

Expand Down
24 changes: 24 additions & 0 deletions src/Native/Firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ var _pairshaped$elm_firebase$Native_Firebase = function () { // eslint-disable-l
return _elm_lang$core$Native_List.fromArray(firebase.apps)
}

var app = function (dummy) {
debug("Firebase.app", dummy)

try {
var app = firebase.app()

if (app) {
return {
ctor: "Just",
_0: {
ctor: "App",
app: function () { return app }
}
}
}
} catch (e) {
// No op
// firebase.app() can throw an error if an app hasn't been initialized.
}

return { ctor: "Nothing" }
}


// Firebase.App methods

Expand Down Expand Up @@ -91,6 +114,7 @@ var _pairshaped$elm_firebase$Native_Firebase = function () { // eslint-disable-l
return {
"sdkVersion": sdkVersion,
"apps": apps,
"app": app,
"init": init,
"initWithName": F2(initWithName),
"deinit": deinit,
Expand Down

0 comments on commit e211703

Please sign in to comment.